/*
 *       This file is part of VFC.
 *
 *        VFC is free software: you can redistribute it and/or modify
 *        it under the terms of the GNU General Public License as published by
 *        the Free Software Foundation, either version 3 of the License, or
 *        (at your option) any later version.
 *
 *        VFC is distributed in the hope that it will be useful,
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *        GNU General Public License for more details.
 *
 *        You should have received a copy of the GNU General Public License
 *        along with VFC.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Text;
using VFC.Debug;
using VFC.Config;

namespace VFC.Nes.Cpu
{
    class Core
    {
        delegate void Execute();
        private static Execute[] RunOpcode = new Execute[256];
        private static int[] Tick = new int[256];
        public static byte[] Size = new byte[256]; //Opcode Size...

        public static int _Opcode_;
        public static int Cycle;
        public static int Param1;
        public static int Param2;

        public static StringBuilder sb = new StringBuilder();

        public static void Init()
        {
            Size[0x00] = 0x01; Tick[0x00] = 0x07; RunOpcode[0x00] = new Execute(Opcode.Implied.BRK);
            Size[0x01] = 0x02; Tick[0x01] = 0x06; RunOpcode[0x01] = new Execute(Opcode.IndirectX.ORA);
            Size[0x02] = 0x01; Tick[0x02] = 0x00; RunOpcode[0x02] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x03] = 0x02; Tick[0x03] = 0x08; RunOpcode[0x03] = new Execute(Opcode.Unofficial.IndirectX.SLO);
            Size[0x04] = 0x02; Tick[0x04] = 0x03; RunOpcode[0x04] = new Execute(Opcode.Unofficial.ZeroPage.DOP);
            Size[0x05] = 0x02; Tick[0x05] = 0x03; RunOpcode[0x05] = new Execute(Opcode.ZeroPage.ORA);
            Size[0x06] = 0x02; Tick[0x06] = 0x05; RunOpcode[0x06] = new Execute(Opcode.ZeroPage.ASL);
            Size[0x07] = 0x02; Tick[0x07] = 0x05; RunOpcode[0x07] = new Execute(Opcode.Unofficial.ZeroPage.SLO);
            Size[0x08] = 0x01; Tick[0x08] = 0x03; RunOpcode[0x08] = new Execute(Opcode.Implied.PHP);
            Size[0x09] = 0x02; Tick[0x09] = 0x02; RunOpcode[0x09] = new Execute(Opcode.Immidiate.ORA);
            Size[0x0a] = 0x01; Tick[0x0a] = 0x02; RunOpcode[0x0a] = new Execute(Opcode.Accumulator.ASL);
            Size[0x0b] = 0x02; Tick[0x0b] = 0x02; RunOpcode[0x0b] = new Execute(Opcode.Unofficial.Immidiate.AAC);
            Size[0x0c] = 0x03; Tick[0x0c] = 0x04; RunOpcode[0x0c] = new Execute(Opcode.Unofficial.Absolute.TOP);
            Size[0x0d] = 0x03; Tick[0x0d] = 0x04; RunOpcode[0x0d] = new Execute(Opcode.Absolute.ORA);
            Size[0x0e] = 0x03; Tick[0x0e] = 0x06; RunOpcode[0x0e] = new Execute(Opcode.Absolute.ASL);
            Size[0x0f] = 0x03; Tick[0x0f] = 0x06; RunOpcode[0x0f] = new Execute(Opcode.Unofficial.Absolute.SLO);

            Size[0x10] = 0x02; Tick[0x10] = 0x02; RunOpcode[0x10] = new Execute(Opcode.Relative.BPL);
            Size[0x11] = 0x02; Tick[0x11] = 0x05; RunOpcode[0x11] = new Execute(Opcode.IndirectY.ORA);
            Size[0x12] = 0x01; Tick[0x12] = 0x00; RunOpcode[0x12] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x13] = 0x02; Tick[0x13] = 0x08; RunOpcode[0x13] = new Execute(Opcode.Unofficial.IndirectY.SLO);
            Size[0x14] = 0x02; Tick[0x14] = 0x04; RunOpcode[0x14] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0x15] = 0x02; Tick[0x15] = 0x04; RunOpcode[0x15] = new Execute(Opcode.ZeroPageX.ORA);
            Size[0x16] = 0x02; Tick[0x16] = 0x06; RunOpcode[0x16] = new Execute(Opcode.ZeroPageX.ASL);
            Size[0x17] = 0x02; Tick[0x17] = 0x06; RunOpcode[0x17] = new Execute(Opcode.Unofficial.ZeroPageX.SLO);
            Size[0x18] = 0x01; Tick[0x18] = 0x02; RunOpcode[0x18] = new Execute(Opcode.Implied.CLC);
            Size[0x19] = 0x03; Tick[0x19] = 0x04; RunOpcode[0x19] = new Execute(Opcode.AbsoluteY.ORA);
            Size[0x1a] = 0x01; Tick[0x1a] = 0x02; RunOpcode[0x1a] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0x1b] = 0x03; Tick[0x1b] = 0x07; RunOpcode[0x1b] = new Execute(Opcode.Unofficial.AbsoluteY.SLO);
            Size[0x1c] = 0x03; Tick[0x1c] = 0x04; RunOpcode[0x1c] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0x1d] = 0x03; Tick[0x1d] = 0x04; RunOpcode[0x1d] = new Execute(Opcode.AbsoluteX.ORA);
            Size[0x1e] = 0x03; Tick[0x1e] = 0x07; RunOpcode[0x1e] = new Execute(Opcode.AbsoluteX.ASL);
            Size[0x1f] = 0x03; Tick[0x1f] = 0x07; RunOpcode[0x1f] = new Execute(Opcode.Unofficial.AbsoluteX.SLO);

            Size[0x20] = 0x03; Tick[0x20] = 0x06; RunOpcode[0x20] = new Execute(Opcode.Absolute.JSR);
            Size[0x21] = 0x02; Tick[0x21] = 0x06; RunOpcode[0x21] = new Execute(Opcode.IndirectX.AND);
            Size[0x22] = 0x01; Tick[0x22] = 0x00; RunOpcode[0x22] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x23] = 0x02; Tick[0x23] = 0x08; RunOpcode[0x23] = new Execute(Opcode.Unofficial.IndirectX.RLA);
            Size[0x24] = 0x02; Tick[0x24] = 0x03; RunOpcode[0x24] = new Execute(Opcode.ZeroPage.BIT);
            Size[0x25] = 0x02; Tick[0x25] = 0x03; RunOpcode[0x25] = new Execute(Opcode.ZeroPage.AND);
            Size[0x26] = 0x02; Tick[0x26] = 0x05; RunOpcode[0x26] = new Execute(Opcode.ZeroPage.ROL);
            Size[0x27] = 0x02; Tick[0x27] = 0x05; RunOpcode[0x27] = new Execute(Opcode.Unofficial.ZeroPage.RLA);
            Size[0x28] = 0x01; Tick[0x28] = 0x04; RunOpcode[0x28] = new Execute(Opcode.Implied.PLP);
            Size[0x29] = 0x02; Tick[0x29] = 0x02; RunOpcode[0x29] = new Execute(Opcode.Immidiate.AND);
            Size[0x2a] = 0x01; Tick[0x2a] = 0x02; RunOpcode[0x2a] = new Execute(Opcode.Accumulator.ROL);
            Size[0x2b] = 0x02; Tick[0x2b] = 0x02; RunOpcode[0x2b] = new Execute(Opcode.Unofficial.Immidiate.AAC);
            Size[0x2c] = 0x03; Tick[0x2c] = 0x04; RunOpcode[0x2c] = new Execute(Opcode.Absolute.BIT);
            Size[0x2d] = 0x03; Tick[0x2d] = 0x04; RunOpcode[0x2d] = new Execute(Opcode.Absolute.AND);
            Size[0x2e] = 0x03; Tick[0x2e] = 0x06; RunOpcode[0x2e] = new Execute(Opcode.Absolute.ROL);
            Size[0x2f] = 0x03; Tick[0x2f] = 0x06; RunOpcode[0x2f] = new Execute(Opcode.Unofficial.Absolute.RLA);

            Size[0x30] = 0x02; Tick[0x30] = 0x02; RunOpcode[0x30] = new Execute(Opcode.Relative.BMI);
            Size[0x31] = 0x02; Tick[0x31] = 0x05; RunOpcode[0x31] = new Execute(Opcode.IndirectY.AND);
            Size[0x32] = 0x01; Tick[0x32] = 0x00; RunOpcode[0x32] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x33] = 0x02; Tick[0x33] = 0x08; RunOpcode[0x33] = new Execute(Opcode.Unofficial.IndirectY.RLA);
            Size[0x34] = 0x02; Tick[0x34] = 0x04; RunOpcode[0x34] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0x35] = 0x02; Tick[0x35] = 0x04; RunOpcode[0x35] = new Execute(Opcode.ZeroPageX.AND);
            Size[0x36] = 0x02; Tick[0x36] = 0x06; RunOpcode[0x36] = new Execute(Opcode.ZeroPageX.ROL);
            Size[0x37] = 0x02; Tick[0x37] = 0x06; RunOpcode[0x37] = new Execute(Opcode.Unofficial.ZeroPageX.RLA);
            Size[0x38] = 0x01; Tick[0x38] = 0x02; RunOpcode[0x38] = new Execute(Opcode.Implied.SEC);
            Size[0x39] = 0x03; Tick[0x39] = 0x04; RunOpcode[0x39] = new Execute(Opcode.AbsoluteY.AND);
            Size[0x3a] = 0x01; Tick[0x3a] = 0x02; RunOpcode[0x3a] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0x3b] = 0x03; Tick[0x3b] = 0x07; RunOpcode[0x3b] = new Execute(Opcode.Unofficial.AbsoluteY.RLA);
            Size[0x3c] = 0x03; Tick[0x3c] = 0x04; RunOpcode[0x3c] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0x3d] = 0x03; Tick[0x3d] = 0x04; RunOpcode[0x3d] = new Execute(Opcode.AbsoluteX.AND);
            Size[0x3e] = 0x03; Tick[0x3e] = 0x07; RunOpcode[0x3e] = new Execute(Opcode.AbsoluteX.ROL);
            Size[0x3f] = 0x03; Tick[0x3f] = 0x07; RunOpcode[0x3f] = new Execute(Opcode.Unofficial.AbsoluteX.RLA);

            Size[0x40] = 0x01; Tick[0x40] = 0x06; RunOpcode[0x40] = new Execute(Opcode.Implied.RTI);
            Size[0x41] = 0x02; Tick[0x41] = 0x06; RunOpcode[0x41] = new Execute(Opcode.IndirectX.EOR);
            Size[0x42] = 0x01; Tick[0x42] = 0x00; RunOpcode[0x42] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x43] = 0x02; Tick[0x43] = 0x08; RunOpcode[0x43] = new Execute(Opcode.Unofficial.IndirectX.SRE);
            Size[0x44] = 0x02; Tick[0x44] = 0x03; RunOpcode[0x44] = new Execute(Opcode.Unofficial.ZeroPage.DOP);
            Size[0x45] = 0x02; Tick[0x45] = 0x03; RunOpcode[0x45] = new Execute(Opcode.ZeroPage.EOR);
            Size[0x46] = 0x02; Tick[0x46] = 0x05; RunOpcode[0x46] = new Execute(Opcode.ZeroPage.LSR);
            Size[0x47] = 0x02; Tick[0x47] = 0x05; RunOpcode[0x47] = new Execute(Opcode.Unofficial.ZeroPage.SRE);
            Size[0x48] = 0x01; Tick[0x48] = 0x03; RunOpcode[0x48] = new Execute(Opcode.Implied.PHA);
            Size[0x49] = 0x02; Tick[0x49] = 0x02; RunOpcode[0x49] = new Execute(Opcode.Immidiate.EOR);
            Size[0x4a] = 0x01; Tick[0x4a] = 0x02; RunOpcode[0x4a] = new Execute(Opcode.Accumulator.LSR);
            Size[0x4b] = 0x02; Tick[0x4b] = 0x02; RunOpcode[0x4b] = new Execute(Opcode.Unofficial.Immidiate.ASR);
            Size[0x4c] = 0x03; Tick[0x4c] = 0x03; RunOpcode[0x4c] = new Execute(Opcode.Absolute.JMP);
            Size[0x4d] = 0x03; Tick[0x4d] = 0x04; RunOpcode[0x4d] = new Execute(Opcode.Absolute.EOR);
            Size[0x4e] = 0x03; Tick[0x4e] = 0x06; RunOpcode[0x4e] = new Execute(Opcode.Absolute.LSR);
            Size[0x4f] = 0x03; Tick[0x4f] = 0x06; RunOpcode[0x4f] = new Execute(Opcode.Unofficial.Absolute.SRE);

            Size[0x50] = 0x02; Tick[0x50] = 0x02; RunOpcode[0x50] = new Execute(Opcode.Relative.BVC);
            Size[0x51] = 0x02; Tick[0x51] = 0x05; RunOpcode[0x51] = new Execute(Opcode.IndirectY.EOR);
            Size[0x52] = 0x01; Tick[0x52] = 0x00; RunOpcode[0x52] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x53] = 0x02; Tick[0x53] = 0x08; RunOpcode[0x53] = new Execute(Opcode.Unofficial.IndirectY.SRE);
            Size[0x54] = 0x02; Tick[0x54] = 0x04; RunOpcode[0x54] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0x55] = 0x02; Tick[0x55] = 0x04; RunOpcode[0x55] = new Execute(Opcode.ZeroPageX.EOR);
            Size[0x56] = 0x02; Tick[0x56] = 0x06; RunOpcode[0x56] = new Execute(Opcode.ZeroPageX.LSR);
            Size[0x57] = 0x02; Tick[0x57] = 0x06; RunOpcode[0x57] = new Execute(Opcode.Unofficial.ZeroPageX.SRE);
            Size[0x58] = 0x01; Tick[0x58] = 0x02; RunOpcode[0x58] = new Execute(Opcode.Implied.CLI);
            Size[0x59] = 0x03; Tick[0x59] = 0x04; RunOpcode[0x59] = new Execute(Opcode.AbsoluteY.EOR);
            Size[0x5a] = 0x01; Tick[0x5a] = 0x02; RunOpcode[0x5a] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0x5b] = 0x03; Tick[0x5b] = 0x07; RunOpcode[0x5b] = new Execute(Opcode.Unofficial.AbsoluteY.SRE);
            Size[0x5c] = 0x03; Tick[0x5c] = 0x04; RunOpcode[0x5c] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0x5d] = 0x03; Tick[0x5d] = 0x04; RunOpcode[0x5d] = new Execute(Opcode.AbsoluteX.EOR);
            Size[0x5e] = 0x03; Tick[0x5e] = 0x07; RunOpcode[0x5e] = new Execute(Opcode.AbsoluteX.LSR);
            Size[0x5f] = 0x03; Tick[0x5f] = 0x07; RunOpcode[0x5f] = new Execute(Opcode.Unofficial.AbsoluteX.SRE);

            Size[0x60] = 0x01; Tick[0x60] = 0x06; RunOpcode[0x60] = new Execute(Opcode.Implied.RTS);
            Size[0x61] = 0x02; Tick[0x61] = 0x06; RunOpcode[0x61] = new Execute(Opcode.IndirectX.ADC);
            Size[0x62] = 0x01; Tick[0x62] = 0x00; RunOpcode[0x62] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x63] = 0x02; Tick[0x63] = 0x08; RunOpcode[0x63] = new Execute(Opcode.Unofficial.IndirectX.RRA);
            Size[0x64] = 0x02; Tick[0x64] = 0x03; RunOpcode[0x64] = new Execute(Opcode.Unofficial.ZeroPage.DOP);
            Size[0x65] = 0x02; Tick[0x65] = 0x03; RunOpcode[0x65] = new Execute(Opcode.ZeroPage.ADC);
            Size[0x66] = 0x02; Tick[0x66] = 0x05; RunOpcode[0x66] = new Execute(Opcode.ZeroPage.ROR);
            Size[0x67] = 0x02; Tick[0x67] = 0x05; RunOpcode[0x67] = new Execute(Opcode.Unofficial.ZeroPage.RRA);
            Size[0x68] = 0x01; Tick[0x68] = 0x04; RunOpcode[0x68] = new Execute(Opcode.Implied.PLA);
            Size[0x69] = 0x02; Tick[0x69] = 0x02; RunOpcode[0x69] = new Execute(Opcode.Immidiate.ADC);
            Size[0x6a] = 0x01; Tick[0x6a] = 0x02; RunOpcode[0x6a] = new Execute(Opcode.Accumulator.ROR);
            Size[0x6b] = 0x02; Tick[0x6b] = 0x02; RunOpcode[0x6b] = new Execute(Opcode.Unofficial.Immidiate.ARR);
            Size[0x6c] = 0x03; Tick[0x6c] = 0x05; RunOpcode[0x6c] = new Execute(Opcode.Indirect.JMP);
            Size[0x6d] = 0x03; Tick[0x6d] = 0x04; RunOpcode[0x6d] = new Execute(Opcode.Absolute.ADC);
            Size[0x6e] = 0x03; Tick[0x6e] = 0x06; RunOpcode[0x6e] = new Execute(Opcode.Absolute.ROR);
            Size[0x6f] = 0x03; Tick[0x6f] = 0x06; RunOpcode[0x6f] = new Execute(Opcode.Unofficial.Absolute.RRA);

            Size[0x70] = 0x02; Tick[0x70] = 0x02; RunOpcode[0x70] = new Execute(Opcode.Relative.BVS);
            Size[0x71] = 0x02; Tick[0x71] = 0x05; RunOpcode[0x71] = new Execute(Opcode.IndirectY.ADC);
            Size[0x72] = 0x01; Tick[0x72] = 0x00; RunOpcode[0x72] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x73] = 0x02; Tick[0x73] = 0x08; RunOpcode[0x73] = new Execute(Opcode.Unofficial.IndirectY.RRA);
            Size[0x74] = 0x02; Tick[0x74] = 0x04; RunOpcode[0x74] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0x75] = 0x02; Tick[0x75] = 0x04; RunOpcode[0x75] = new Execute(Opcode.ZeroPageX.ADC);
            Size[0x76] = 0x02; Tick[0x76] = 0x06; RunOpcode[0x76] = new Execute(Opcode.ZeroPageX.ROR);
            Size[0x77] = 0x02; Tick[0x77] = 0x06; RunOpcode[0x77] = new Execute(Opcode.Unofficial.ZeroPageX.RRA);
            Size[0x78] = 0x01; Tick[0x78] = 0x02; RunOpcode[0x78] = new Execute(Opcode.Implied.SEI);
            Size[0x79] = 0x03; Tick[0x79] = 0x04; RunOpcode[0x79] = new Execute(Opcode.AbsoluteY.ADC);
            Size[0x7a] = 0x01; Tick[0x7a] = 0x02; RunOpcode[0x7a] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0x7b] = 0x03; Tick[0x7b] = 0x07; RunOpcode[0x7b] = new Execute(Opcode.Unofficial.AbsoluteY.RRA);
            Size[0x7c] = 0x03; Tick[0x7c] = 0x04; RunOpcode[0x7c] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0x7d] = 0x03; Tick[0x7d] = 0x04; RunOpcode[0x7d] = new Execute(Opcode.AbsoluteX.ADC);
            Size[0x7e] = 0x03; Tick[0x7e] = 0x07; RunOpcode[0x7e] = new Execute(Opcode.AbsoluteX.ROR);
            Size[0x7f] = 0x03; Tick[0x7f] = 0x07; RunOpcode[0x7f] = new Execute(Opcode.Unofficial.AbsoluteX.RRA);

            Size[0x80] = 0x02; Tick[0x80] = 0x02; RunOpcode[0x80] = new Execute(Opcode.Unofficial.Immidiate.DOP);
            Size[0x81] = 0x02; Tick[0x81] = 0x06; RunOpcode[0x81] = new Execute(Opcode.IndirectX.STA);
            Size[0x82] = 0x02; Tick[0x82] = 0x02; RunOpcode[0x82] = new Execute(Opcode.Unofficial.Immidiate.DOP);
            Size[0x83] = 0x02; Tick[0x83] = 0x06; RunOpcode[0x83] = new Execute(Opcode.Unofficial.IndirectX.AAX);
            Size[0x84] = 0x02; Tick[0x84] = 0x03; RunOpcode[0x84] = new Execute(Opcode.ZeroPage.STY);
            Size[0x85] = 0x02; Tick[0x85] = 0x03; RunOpcode[0x85] = new Execute(Opcode.ZeroPage.STA);
            Size[0x86] = 0x02; Tick[0x86] = 0x03; RunOpcode[0x86] = new Execute(Opcode.ZeroPage.STX);
            Size[0x87] = 0x02; Tick[0x87] = 0x03; RunOpcode[0x87] = new Execute(Opcode.Unofficial.ZeroPage.AAX);
            Size[0x88] = 0x01; Tick[0x88] = 0x02; RunOpcode[0x88] = new Execute(Opcode.Implied.DEY);
            Size[0x89] = 0x02; Tick[0x89] = 0x02; RunOpcode[0x89] = new Execute(Opcode.Unofficial.Immidiate.DOP);
            Size[0x8a] = 0x01; Tick[0x8a] = 0x02; RunOpcode[0x8a] = new Execute(Opcode.Implied.TXA);
            Size[0x8b] = 0x02; Tick[0x8b] = 0x02; RunOpcode[0x8b] = new Execute(Opcode.Unofficial.Immidiate.XAA);
            Size[0x8c] = 0x03; Tick[0x8c] = 0x04; RunOpcode[0x8c] = new Execute(Opcode.Absolute.STY);
            Size[0x8d] = 0x03; Tick[0x8d] = 0x04; RunOpcode[0x8d] = new Execute(Opcode.Absolute.STA);
            Size[0x8e] = 0x03; Tick[0x8e] = 0x04; RunOpcode[0x8e] = new Execute(Opcode.Absolute.STX);
            Size[0x8f] = 0x03; Tick[0x8f] = 0x04; RunOpcode[0x8f] = new Execute(Opcode.Unofficial.Absolute.AAX);

            Size[0x90] = 0x02; Tick[0x90] = 0x02; RunOpcode[0x90] = new Execute(Opcode.Relative.BCC);
            Size[0x91] = 0x02; Tick[0x91] = 0x06; RunOpcode[0x91] = new Execute(Opcode.IndirectY.STA);
            Size[0x92] = 0x01; Tick[0x92] = 0x00; RunOpcode[0x92] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0x93] = 0x02; Tick[0x93] = 0x06; RunOpcode[0x93] = new Execute(Opcode.Unofficial.IndirectY.AXA);
            Size[0x94] = 0x02; Tick[0x94] = 0x04; RunOpcode[0x94] = new Execute(Opcode.ZeroPageX.STY);
            Size[0x95] = 0x02; Tick[0x95] = 0x04; RunOpcode[0x95] = new Execute(Opcode.ZeroPageX.STA);
            Size[0x96] = 0x02; Tick[0x96] = 0x04; RunOpcode[0x96] = new Execute(Opcode.ZeroPageY.STX);
            Size[0x97] = 0x02; Tick[0x97] = 0x04; RunOpcode[0x97] = new Execute(Opcode.Unofficial.ZeroPageY.AAX);
            Size[0x98] = 0x01; Tick[0x98] = 0x02; RunOpcode[0x98] = new Execute(Opcode.Implied.TYA);
            Size[0x99] = 0x03; Tick[0x99] = 0x05; RunOpcode[0x99] = new Execute(Opcode.AbsoluteY.STA);
            Size[0x9a] = 0x01; Tick[0x9a] = 0x02; RunOpcode[0x9a] = new Execute(Opcode.Implied.TXS);
            Size[0x9b] = 0x03; Tick[0x9b] = 0x05; RunOpcode[0x9b] = new Execute(Opcode.Unofficial.AbsoluteY.XAS);
            Size[0x9c] = 0x03; Tick[0x9c] = 0x05; RunOpcode[0x9c] = new Execute(Opcode.Unofficial.AbsoluteX.SYA);
            Size[0x9d] = 0x03; Tick[0x9d] = 0x05; RunOpcode[0x9d] = new Execute(Opcode.AbsoluteX.STA);
            Size[0x9e] = 0x03; Tick[0x9e] = 0x05; RunOpcode[0x9e] = new Execute(Opcode.Unofficial.AbsoluteY.SXA);
            Size[0x9f] = 0x03; Tick[0x9f] = 0x05; RunOpcode[0x9f] = new Execute(Opcode.Unofficial.AbsoluteY.AXA);

            Size[0xa0] = 0x02; Tick[0xa0] = 0x02; RunOpcode[0xa0] = new Execute(Opcode.Immidiate.LDY);
            Size[0xa1] = 0x02; Tick[0xa1] = 0x06; RunOpcode[0xa1] = new Execute(Opcode.IndirectX.LDA);
            Size[0xa2] = 0x02; Tick[0xa2] = 0x02; RunOpcode[0xa2] = new Execute(Opcode.Immidiate.LDX);
            Size[0xa3] = 0x02; Tick[0xa3] = 0x06; RunOpcode[0xa3] = new Execute(Opcode.Unofficial.IndirectX.LAX);
            Size[0xa4] = 0x02; Tick[0xa4] = 0x03; RunOpcode[0xa4] = new Execute(Opcode.ZeroPage.LDY);
            Size[0xa5] = 0x02; Tick[0xa5] = 0x03; RunOpcode[0xa5] = new Execute(Opcode.ZeroPage.LDA);
            Size[0xa6] = 0x02; Tick[0xa6] = 0x03; RunOpcode[0xa6] = new Execute(Opcode.ZeroPage.LDX);
            Size[0xa7] = 0x02; Tick[0xa7] = 0x03; RunOpcode[0xa7] = new Execute(Opcode.Unofficial.ZeroPage.LAX);
            Size[0xa8] = 0x01; Tick[0xa8] = 0x02; RunOpcode[0xa8] = new Execute(Opcode.Implied.TAY);
            Size[0xa9] = 0x02; Tick[0xa9] = 0x02; RunOpcode[0xa9] = new Execute(Opcode.Immidiate.LDA);
            Size[0xaa] = 0x01; Tick[0xaa] = 0x02; RunOpcode[0xaa] = new Execute(Opcode.Implied.TAX);
            Size[0xab] = 0x02; Tick[0xab] = 0x02; RunOpcode[0xab] = new Execute(Opcode.Unofficial.Immidiate.ATX);
            Size[0xac] = 0x03; Tick[0xac] = 0x04; RunOpcode[0xac] = new Execute(Opcode.Absolute.LDY);
            Size[0xad] = 0x03; Tick[0xad] = 0x04; RunOpcode[0xad] = new Execute(Opcode.Absolute.LDA);
            Size[0xae] = 0x03; Tick[0xae] = 0x04; RunOpcode[0xae] = new Execute(Opcode.Absolute.LDX);
            Size[0xaf] = 0x03; Tick[0xaf] = 0x04; RunOpcode[0xaf] = new Execute(Opcode.Unofficial.Absolute.LAX);

            Size[0xb0] = 0x02; Tick[0xb0] = 0x02; RunOpcode[0xb0] = new Execute(Opcode.Relative.BCS);
            Size[0xb1] = 0x02; Tick[0xb1] = 0x05; RunOpcode[0xb1] = new Execute(Opcode.IndirectY.LDA);
            Size[0xb2] = 0x01; Tick[0xb2] = 0x00; RunOpcode[0xb2] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0xb3] = 0x02; Tick[0xb3] = 0x05; RunOpcode[0xb3] = new Execute(Opcode.Unofficial.IndirectY.LAX);
            Size[0xb4] = 0x02; Tick[0xb4] = 0x04; RunOpcode[0xb4] = new Execute(Opcode.ZeroPageX.LDY);
            Size[0xb5] = 0x02; Tick[0xb5] = 0x04; RunOpcode[0xb5] = new Execute(Opcode.ZeroPageX.LDA);
            Size[0xb6] = 0x02; Tick[0xb6] = 0x04; RunOpcode[0xb6] = new Execute(Opcode.ZeroPageY.LDX);
            Size[0xb7] = 0x02; Tick[0xb7] = 0x04; RunOpcode[0xb7] = new Execute(Opcode.Unofficial.ZeroPageY.LAX);
            Size[0xb8] = 0x01; Tick[0xb8] = 0x02; RunOpcode[0xb8] = new Execute(Opcode.Implied.CLV);
            Size[0xb9] = 0x03; Tick[0xb9] = 0x04; RunOpcode[0xb9] = new Execute(Opcode.AbsoluteY.LDA);
            Size[0xba] = 0x01; Tick[0xba] = 0x02; RunOpcode[0xba] = new Execute(Opcode.Implied.TSX);
            Size[0xbb] = 0x03; Tick[0xbb] = 0x04; RunOpcode[0xbb] = new Execute(Opcode.Unofficial.AbsoluteY.LAR);
            Size[0xbc] = 0x03; Tick[0xbc] = 0x04; RunOpcode[0xbc] = new Execute(Opcode.AbsoluteX.LDY);
            Size[0xbd] = 0x03; Tick[0xbd] = 0x04; RunOpcode[0xbd] = new Execute(Opcode.AbsoluteX.LDA);
            Size[0xbe] = 0x03; Tick[0xbe] = 0x04; RunOpcode[0xbe] = new Execute(Opcode.AbsoluteY.LDX);
            Size[0xbf] = 0x03; Tick[0xbf] = 0x04; RunOpcode[0xbf] = new Execute(Opcode.Unofficial.AbsoluteY.LAX);

            Size[0xc0] = 0x02; Tick[0xc0] = 0x02; RunOpcode[0xc0] = new Execute(Opcode.Immidiate.CPY);
            Size[0xc1] = 0x02; Tick[0xc1] = 0x06; RunOpcode[0xc1] = new Execute(Opcode.IndirectX.CMP);
            Size[0xc2] = 0x02; Tick[0xc2] = 0x02; RunOpcode[0xc2] = new Execute(Opcode.Unofficial.Immidiate.DOP);
            Size[0xc3] = 0x02; Tick[0xc3] = 0x08; RunOpcode[0xc3] = new Execute(Opcode.Unofficial.IndirectX.DCP);
            Size[0xc4] = 0x02; Tick[0xc4] = 0x03; RunOpcode[0xc4] = new Execute(Opcode.ZeroPage.CPY);
            Size[0xc5] = 0x02; Tick[0xc5] = 0x03; RunOpcode[0xc5] = new Execute(Opcode.ZeroPage.CMP);
            Size[0xc6] = 0x02; Tick[0xc6] = 0x05; RunOpcode[0xc6] = new Execute(Opcode.ZeroPage.DEC);
            Size[0xc7] = 0x02; Tick[0xc7] = 0x05; RunOpcode[0xc7] = new Execute(Opcode.Unofficial.ZeroPage.DCP);
            Size[0xc8] = 0x01; Tick[0xc8] = 0x02; RunOpcode[0xc8] = new Execute(Opcode.Implied.INY);
            Size[0xc9] = 0x02; Tick[0xc9] = 0x02; RunOpcode[0xc9] = new Execute(Opcode.Immidiate.CMP);
            Size[0xca] = 0x01; Tick[0xca] = 0x02; RunOpcode[0xca] = new Execute(Opcode.Implied.DEX);
            Size[0xcb] = 0x02; Tick[0xcb] = 0x02; RunOpcode[0xcb] = new Execute(Opcode.Unofficial.Immidiate.AXS);
            Size[0xcc] = 0x03; Tick[0xcc] = 0x04; RunOpcode[0xcc] = new Execute(Opcode.Absolute.CPY);
            Size[0xcd] = 0x03; Tick[0xcd] = 0x04; RunOpcode[0xcd] = new Execute(Opcode.Absolute.CMP);
            Size[0xce] = 0x03; Tick[0xce] = 0x06; RunOpcode[0xce] = new Execute(Opcode.Absolute.DEC);
            Size[0xcf] = 0x03; Tick[0xcf] = 0x06; RunOpcode[0xcf] = new Execute(Opcode.Unofficial.Absolute.DCP);

            Size[0xd0] = 0x02; Tick[0xd0] = 0x02; RunOpcode[0xd0] = new Execute(Opcode.Relative.BNE);
            Size[0xd1] = 0x02; Tick[0xd1] = 0x05; RunOpcode[0xd1] = new Execute(Opcode.IndirectY.CMP);
            Size[0xd2] = 0x01; Tick[0xd2] = 0x00; RunOpcode[0xd2] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0xd3] = 0x02; Tick[0xd3] = 0x08; RunOpcode[0xd3] = new Execute(Opcode.Unofficial.IndirectY.DCP);
            Size[0xd4] = 0x02; Tick[0xd4] = 0x04; RunOpcode[0xd4] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0xd5] = 0x02; Tick[0xd5] = 0x04; RunOpcode[0xd5] = new Execute(Opcode.ZeroPageX.CMP);
            Size[0xd6] = 0x02; Tick[0xd6] = 0x06; RunOpcode[0xd6] = new Execute(Opcode.ZeroPageX.DEC);
            Size[0xd7] = 0x02; Tick[0xd7] = 0x06; RunOpcode[0xd7] = new Execute(Opcode.Unofficial.ZeroPageX.DCP);
            Size[0xd8] = 0x01; Tick[0xd8] = 0x02; RunOpcode[0xd8] = new Execute(Opcode.Implied.CLD);
            Size[0xd9] = 0x03; Tick[0xd9] = 0x04; RunOpcode[0xd9] = new Execute(Opcode.AbsoluteY.CMP);
            Size[0xda] = 0x01; Tick[0xda] = 0x02; RunOpcode[0xda] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0xdb] = 0x03; Tick[0xdb] = 0x07; RunOpcode[0xdb] = new Execute(Opcode.Unofficial.AbsoluteY.DCP);
            Size[0xdc] = 0x03; Tick[0xdc] = 0x04; RunOpcode[0xdc] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0xdd] = 0x03; Tick[0xdd] = 0x04; RunOpcode[0xdd] = new Execute(Opcode.AbsoluteX.CMP);
            Size[0xde] = 0x03; Tick[0xde] = 0x07; RunOpcode[0xde] = new Execute(Opcode.AbsoluteX.DEC);
            Size[0xdf] = 0x03; Tick[0xdf] = 0x07; RunOpcode[0xdf] = new Execute(Opcode.Unofficial.AbsoluteX.DCP);

            Size[0xe0] = 0x02; Tick[0xe0] = 0x02; RunOpcode[0xe0] = new Execute(Opcode.Immidiate.CPX);
            Size[0xe1] = 0x02; Tick[0xe1] = 0x06; RunOpcode[0xe1] = new Execute(Opcode.IndirectX.SBC);
            Size[0xe2] = 0x02; Tick[0xe2] = 0x02; RunOpcode[0xe2] = new Execute(Opcode.Unofficial.Immidiate.DOP);
            Size[0xe3] = 0x02; Tick[0xe3] = 0x08; RunOpcode[0xe3] = new Execute(Opcode.Unofficial.IndirectX.ISC);
            Size[0xe4] = 0x02; Tick[0xe4] = 0x03; RunOpcode[0xe4] = new Execute(Opcode.ZeroPage.CPX);
            Size[0xe5] = 0x02; Tick[0xe5] = 0x03; RunOpcode[0xe5] = new Execute(Opcode.ZeroPage.SBC);
            Size[0xe6] = 0x02; Tick[0xe6] = 0x05; RunOpcode[0xe6] = new Execute(Opcode.ZeroPage.INC);
            Size[0xe7] = 0x02; Tick[0xe7] = 0x05; RunOpcode[0xe7] = new Execute(Opcode.Unofficial.ZeroPage.ISC);
            Size[0xe8] = 0x01; Tick[0xe8] = 0x02; RunOpcode[0xe8] = new Execute(Opcode.Implied.INX);
            Size[0xe9] = 0x02; Tick[0xe9] = 0x02; RunOpcode[0xe9] = new Execute(Opcode.Immidiate.SBC);
            Size[0xea] = 0x01; Tick[0xea] = 0x02; RunOpcode[0xea] = new Execute(Opcode.Implied.NOP);
            Size[0xeb] = 0x02; Tick[0xeb] = 0x02; RunOpcode[0xeb] = new Execute(Opcode.Unofficial.Immidiate.SBC);
            Size[0xec] = 0x03; Tick[0xec] = 0x04; RunOpcode[0xec] = new Execute(Opcode.Absolute.CPX);
            Size[0xed] = 0x03; Tick[0xed] = 0x04; RunOpcode[0xed] = new Execute(Opcode.Absolute.SBC);
            Size[0xee] = 0x03; Tick[0xee] = 0x06; RunOpcode[0xee] = new Execute(Opcode.Absolute.INC);
            Size[0xef] = 0x03; Tick[0xef] = 0x06; RunOpcode[0xef] = new Execute(Opcode.Unofficial.Absolute.ISC);

            Size[0xf0] = 0x02; Tick[0xf0] = 0x02; RunOpcode[0xf0] = new Execute(Opcode.Relative.BEQ);
            Size[0xf1] = 0x02; Tick[0xf1] = 0x05; RunOpcode[0xf1] = new Execute(Opcode.IndirectY.SBC);
            Size[0xf2] = 0x01; Tick[0xf2] = 0x00; RunOpcode[0xf2] = new Execute(Opcode.Unofficial.Implied.KIL);
            Size[0xf3] = 0x02; Tick[0xf3] = 0x08; RunOpcode[0xf3] = new Execute(Opcode.Unofficial.IndirectY.ISC);
            Size[0xf4] = 0x02; Tick[0xf4] = 0x04; RunOpcode[0xf4] = new Execute(Opcode.Unofficial.ZeroPageX.DOP);
            Size[0xf5] = 0x02; Tick[0xf5] = 0x04; RunOpcode[0xf5] = new Execute(Opcode.ZeroPageX.SBC);
            Size[0xf6] = 0x02; Tick[0xf6] = 0x06; RunOpcode[0xf6] = new Execute(Opcode.ZeroPageX.INC);
            Size[0xf7] = 0x02; Tick[0xf7] = 0x06; RunOpcode[0xf7] = new Execute(Opcode.Unofficial.ZeroPageX.ISC);
            Size[0xf8] = 0x01; Tick[0xf8] = 0x02; RunOpcode[0xf8] = new Execute(Opcode.Implied.SED);
            Size[0xf9] = 0x03; Tick[0xf9] = 0x04; RunOpcode[0xf9] = new Execute(Opcode.AbsoluteY.SBC);
            Size[0xfa] = 0x01; Tick[0xfa] = 0x02; RunOpcode[0xfa] = new Execute(Opcode.Unofficial.Implied.NOP);
            Size[0xfb] = 0x03; Tick[0xfb] = 0x07; RunOpcode[0xfb] = new Execute(Opcode.Unofficial.AbsoluteY.ISC);
            Size[0xfc] = 0x03; Tick[0xfc] = 0x04; RunOpcode[0xfc] = new Execute(Opcode.Unofficial.AbsoluteX.TOP);
            Size[0xfd] = 0x03; Tick[0xfd] = 0x04; RunOpcode[0xfd] = new Execute(Opcode.AbsoluteX.SBC);
            Size[0xfe] = 0x03; Tick[0xfe] = 0x07; RunOpcode[0xfe] = new Execute(Opcode.AbsoluteX.INC);
            Size[0xff] = 0x03; Tick[0xff] = 0x07; RunOpcode[0xff] = new Execute(Opcode.Unofficial.AbsoluteX.ISC);
        }

        public static void Exec()
        {
            Param1 = 0;
            Param2 = 0;

            _Opcode_ = Cpu.Memory.Read(Cpu.Register.PC);

            if (Size[_Opcode_] == 0x03)
            {
                Param1 = Cpu.Memory.Read(Cpu.Register.PC + 1);
                Param2 = Cpu.Memory.Read(Cpu.Register.PC + 2);
            }
            else if (Size[_Opcode_] == 0x02)
            {
                Param1 = Cpu.Memory.Read(Cpu.Register.PC + 1);
                Param2 = 0;
            }

            //Opcode Logging............
            if (App.EnableOpcodeLogging) 
            {                                              
                sb.Append(Cpu.Register.PC.ToString("X2")); 
                sb.Append(" ");                            
                sb.Append(_Opcode_.ToString("X2"));        
                sb.Append(" ");                           
                sb.Append(Param1.ToString("X2"));        
                sb.Append(" ");  
                        
                if (Size[_Opcode_] == 0x03)             
                {                                      
                    sb.Append(Param2.ToString("X2"));   
                    sb.Append("  ");                      
                    sb.Append(Disassembler.Decode(_Opcode_));
                    if (Disassembler.Mode[_Opcode_] == Disassembler.ABSOLUTE)
                    {
                        sb.Append(" $");
                        sb.Append(Param2.ToString("X2"));
                        sb.Append(Param1.ToString("X2"));
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.ABSOLUTE_X)
                    {
                        sb.Append(" $");
                        sb.Append(Param2.ToString("X2"));
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(",X");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.ABSOLUTE_Y)
                    {
                        sb.Append(" $");
                        sb.Append(Param2.ToString("X2"));
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(",Y");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.INDIRECT)
                    {
                        sb.Append(" ($");
                        sb.Append(Param2.ToString("X2"));
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(")");
                    }
                }                                         
                else if (Size[_Opcode_] == 0x02)           
                {                                         
                    sb.Append("    ");
                    sb.Append(Disassembler.Decode(_Opcode_));
                    if (Disassembler.Mode[_Opcode_] == Disassembler.ZERO_PAGE)
                    {
                        sb.Append(" $");
                        sb.Append(Param1.ToString("X2") + "  ");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.ZERO_PAGE_X)
                    {
                        sb.Append(" $");
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(",X");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.ZERO_PAGE_Y)
                    {
                        sb.Append(" $");
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(",Y");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.INDIRECT_X)
                    {
                        sb.Append(" ($");
                        sb.Append(Param1.ToString("X2"));
                        sb.Append(",X)");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.INDIRECT_Y)
                    {
                        sb.Append(" ($");
                        sb.Append(Param1.ToString("X2"));
                        sb.Append("),Y");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.IMMIDIATE)
                    {
                        sb.Append(" #$");
                        sb.Append(Param1.ToString("X2") + " ");
                    }
                    else if (Disassembler.Mode[_Opcode_] == Disassembler.RELATIVE)
                    {
                        sb.Append(" $");
                        sb.Append(Param1.ToString("X2") + "  ");
                    }
                }
                else if (Size[_Opcode_] == 0x01)
                {
                    if (Disassembler.Mode[_Opcode_] == Disassembler.ACCUMULATOR)
                    {
                        sb.Append("    ");
                        sb.Append(Disassembler.Decode(_Opcode_));
                        sb.Append(" A    ");
                    }
                    else
                    {
                        sb.Append("    ");
                        sb.Append(Disassembler.Decode(_Opcode_) + "      ");
                    }
                }
                sb.Append("           ");
                sb.Append("A:" + Cpu.Register.A.ToString("X2"));
                sb.Append(" ");
                sb.Append("X:" + Cpu.Register.X.ToString("X2"));
                sb.Append(" ");
                sb.Append("Y:" + Cpu.Register.Y.ToString("X2"));
                sb.Append(" ");
                sb.Append("P:" + Cpu.Register.SR.ToString("X2"));
                sb.Append(" ");
                sb.Append("SP:" + Cpu.Register.SP.ToString("X2"));
                sb.Append(" ");
                sb.Append("CYC:" + Nes.Counter.CpuCycle.ToString());

                Emu.Log.Append(sb.ToString());             
                sb.Length = 0;                            
            } 

            Cycle = Tick[_Opcode_];
            RunOpcode[_Opcode_]();
            Nes.Counter.CpuCycle += Cycle;
        }
    }
}
